layui.use(['form', 'table'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table;

    table.render({
        elem: '#currentTableId',
        url: '../OrderTemp/list',
        toolbar: '#toolbarDemo',
        defaultToolbar: ['filter', 'exports', 'print',
            {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }
        ],
        page: true,
        cols: [[
            {type: "checkbox", width: 50},
            // {field: 'id', width: 80, title: 'ID', sort: true},
            {field: 'temp_name', width: 200, title: '模板名'},
            {field: 'temp_uid', width: 120, title: '模板UID', hide:true},
            {field: 'dealer', width: 200, title: '经销商'},
            {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
        ]],
        limits: [25, 50, 100],
        limit: 25,
        // page: true,
        skin: 'line'
    });

    // 监听搜索操作
    form.on('submit(data-search-btn)', function (data) {
        var result = JSON.stringify(data.field);
        layer.alert(result, {
            title: '最终的搜索信息'
        });

        //执行搜索重载
        table.reload('currentTableId', {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });

    /**
     * toolbar监听事件
     */
    table.on('toolbar(currentTableFilter)', function (obj) {
        if (obj.event === 'import') {  // 监听导入操作
            var index = layer.open({
                title: '导入订单模板',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '../../page/OrderTemp-import.html',
                end: function(){
                    table.reload('currentTableId');
                },
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        } else if (obj.event === 'delete') {  // 监听删除操作
            
            var checkStatus = table.checkStatus('currentTableId')
                        , data = checkStatus.data;

            var tempname ;
            for (x = 0;x<data.length;x++){
                if (typeof(tempname) == "undefined"){
                    // console.log("debug tempname:",tempname);
                    tempname = data[x].excel_name;
                }else{
                    tempname = tempname + "," +data[x].excel_name;
                }
            }
            layer.confirm("确认删除？？",{
                title:"确认删除",
                end: function () {
                    table.reload('currentTableId');
                },
            },function(index){
                $.ajax({
                    url:'/OrderTemp/del',
                    type:"post",
                    data:JSON.stringify(data),
                    dataType:"json",
                    contentType: 'application/json',  // 声明发送数据格式
                });
                layer.close(index);
            });
        }
    });

    //监听表格复选框选择
    table.on('checkbox(currentTableFilter)', function (obj) {
        // console.log(obj)
    });

    table.on('tool(currentTableFilter)', function (obj) {
        var data = obj.data;
        if (obj.event === 'view') {
            $.ajax({
                url:'/OrderTemp/view',
                type:"get",
                data:"id="+data.temp_uid,
                dataType:"json",
                contentType:'application/json',  // 声明发送数据格式
                success:(res)=>{

                    var tempname ;
                    for (x = 0;x<res.length;x++){
                        if (typeof(tempname) == "undefined"){
                            tempname = res[x];
                        }else{
                            tempname = tempname + "</br>" +res[x];
                        }
                    }

                    layer.alert(tempname,{
                        title: "<h2>" + data.temp_name + "</h2>",
                        end: function () {
                            table.reload('currentTableId');
                        },
                    },function(index){
                        layer.close(index);
                    });
                }
            });
        } else if (obj.event === 'delete') {

        }
    });

});